#include "weatherwindow.h"
#include "ui_weatherwindow.h"



WeatherWindow::WeatherWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::WeatherWindow)
{
    ui->setupUi(this);
    show_back_gif();
    QStringList items;
    items << "武汉" << "黄石" << "十堰" <<"广州"
          << "北京" << "天津" << "石家庄" << "太原"
          << "呼和浩特" << "沈阳" << "长春" << "哈尔滨"
          << "上海" << "南京" << "杭州" << "合肥"
          << "福州" << "南昌" << "济南" << "郑州";
    ui->listWidget->addItems(items);
    ui->listWidget->setSpacing(10);  // 设置条目之间的间距为10像素
        // 1. 准备请求参数
        m_manager = new QNetworkAccessManager(this);
        connect(m_manager, &QNetworkAccessManager::finished, this, &WeatherWindow::on_weather_reply);
        QString cityName = "广州"; // 假设有一个城市输入框
        set_city(cityName);
}

WeatherWindow::~WeatherWindow()
{
    delete ui;
}

//处理请求响应的槽函数
void WeatherWindow::on_weather_reply(QNetworkReply *reply)
{
    QByteArray data = reply->readAll();
    qDebug()<<QString(data);

    //获取Json数据
    QJsonDocument doc = QJsonDocument::fromJson(data);

    QJsonObject rootObj = doc.object();

    // 解析results数组
    QJsonArray resultsArray = rootObj.value("results").toArray();
    if(resultsArray.isEmpty()) {
        qDebug()<<"未获取到天气数据";
    }

    // 获取第一个结果对象
    QJsonObject resultObj = resultsArray.first().toObject();

    // 获取location和now对象
    QJsonObject locationObj = resultObj.value("location").toObject();
    QJsonObject nowObj = resultObj.value("now").toObject();

    // 提取具体数据
    QString city = locationObj.value("name").toString();
    QString weather = nowObj.value("text").toString();
    QString temperature = nowObj.value("temperature").toString();
    QString code = nowObj.value("code").toString();

    ui->citylabel->setText(city);
    ui->weatherlabel->setText(weather);
    ui->Tlabel->setText(temperature+"°C");

    set_weathercode(code.toUInt());
    qDebug()<<"城市:"<<city;
    qDebug()<<"天气:"<<weather;
    qDebug()<<"温度:"<<temperature<<"°C";
    qDebug()<<"气象代码:"<<code;
}

//显示动态背景图
void WeatherWindow::show_back_gif()
{
    QMovie *movepic = new QMovie(QString::fromLocal8Bit(":/new/prefix1/back.gif"));
    movepic->setScaledSize(ui->backGif->size());
    ui->backGif->setMovie(movepic);
    movepic->start();
}

//设置城市并请求获取该城市的天气
void WeatherWindow::set_city(QString name)
{
    QString url = QString("http://api.seniverse.com/v3/weather/now.json?"
                                 "key=SfgFskzCppFmWqRq1&"
                                 "location=%1&"
                                 "language=zh-Hans&"
                                 "unit=c").arg(name);

    m_manager->get(QNetworkRequest(url));
}

//根据天气气象代码，显示对应的图片（40个气象代码对应40个图片）
void WeatherWindow::set_weathercode(int code)
{
    switch (code) {
    case 0:ui->codelabel->setStyleSheet("border-image: url(:/new/prefix1/codeblack/02x (1).png);");
        break;
    case 1:ui->codelabel->setStyleSheet("border-image: url(:/new/prefix1/codeblack/02x (2).png);");
        break;
    case 2:ui->codelabel->setStyleSheet("border-image: url(:/new/prefix1/codeblack/02x (3).png);");
        break;
    case 3:ui->codelabel->setStyleSheet("border-image: url(:/new/prefix1/codeblack/02x (4).png);");
        break;
    case 4:ui->codelabel->setStyleSheet("border-image: url(:/new/prefix1/codeblack/02x (5).png);");
        break;
    case 5:ui->codelabel->setStyleSheet("border-image: url(:/new/prefix1/codeblack/02x (6).png);");
        break;
    case 6:ui->codelabel->setStyleSheet("border-image: url(:/new/prefix1/codeblack/02x (7).png);");
        break;
    case 7:ui->codelabel->setStyleSheet("border-image: url(:/new/prefix1/codeblack/02x (8).png);");
        break;
    case 8:ui->codelabel->setStyleSheet("border-image: url(:/new/prefix1/codeblack/02x (9).png);");
        break;
    case 9:ui->codelabel->setStyleSheet("border-image: url(:/new/prefix1/codeblack/02x (10).png);");
        break;
    case 10:ui->codelabel->setStyleSheet("border-image: url(:/new/prefix1/codeblack/02x (11).png);");
        break;
    case 11:ui->codelabel->setStyleSheet("border-image: url(:/new/prefix1/codeblack/02x (12).png);");
        break;
    case 12:ui->codelabel->setStyleSheet("border-image: url(:/new/prefix1/codeblack/02x (13).png);");
        break;
    case 13:ui->codelabel->setStyleSheet("border-image: url(:/new/prefix1/codeblack/02x (14).png);");
        break;
    case 14:ui->codelabel->setStyleSheet("border-image: url(:/new/prefix1/codeblack/02x (15).png);");
        break;
    case 15:ui->codelabel->setStyleSheet("border-image: url(:/new/prefix1/codeblack/02x (16).png);");
        break;
    case 16:ui->codelabel->setStyleSheet("border-image: url(:/new/prefix1/codeblack/02x (17).png);");
        break;
    case 17:ui->codelabel->setStyleSheet("border-image: url(:/new/prefix1/codeblack/02x (18).png);");
        break;
    case 18:ui->codelabel->setStyleSheet("border-image: url(:/new/prefix1/codeblack/02x (19).png);");
        break;
    case 19:ui->codelabel->setStyleSheet("border-image: url(:/new/prefix1/codeblack/02x (20).png);");
        break;
    case 20:ui->codelabel->setStyleSheet("border-image: url(:/new/prefix1/codeblack/02x (21).png);");
        break;
    case 21:ui->codelabel->setStyleSheet("border-image: url(:/new/prefix1/codeblack/02x (22).png);");
        break;
    case 22:ui->codelabel->setStyleSheet("border-image: url(:/new/prefix1/codeblack/02x (23).png);");
        break;
    case 23:ui->codelabel->setStyleSheet("border-image: url(:/new/prefix1/codeblack/02x (24).png);");
        break;
    case 24:ui->codelabel->setStyleSheet("border-image: url(:/new/prefix1/codeblack/02x (25).png);");
        break;
    case 25:ui->codelabel->setStyleSheet("border-image: url(:/new/prefix1/codeblack/02x (26).png);");
        break;
    case 26:ui->codelabel->setStyleSheet("border-image: url(:/new/prefix1/codeblack/02x (27).png);");
        break;
    case 27:ui->codelabel->setStyleSheet("border-image: url(:/new/prefix1/codeblack/02x (28).png);");
        break;
    case 28:ui->codelabel->setStyleSheet("border-image: url(:/new/prefix1/codeblack/02x (29).png);");
        break;
    case 29:ui->codelabel->setStyleSheet("border-image: url(:/new/prefix1/codeblack/02x (30).png);");
        break;
    case 30:ui->codelabel->setStyleSheet("border-image: url(:/new/prefix1/codeblack/02x (31).png);");
        break;
    case 31:ui->codelabel->setStyleSheet("border-image: url(:/new/prefix1/codeblack/02x (32).png);");
        break;
    case 32:ui->codelabel->setStyleSheet("border-image: url(:/new/prefix1/codeblack/02x (33).png);");
        break;
    case 33:ui->codelabel->setStyleSheet("border-image: url(:/new/prefix1/codeblack/02x (34).png);");
        break;
    case 34:ui->codelabel->setStyleSheet("border-image: url(:/new/prefix1/codeblack/02x (35).png);");
        break;
    case 35:ui->codelabel->setStyleSheet("border-image: url(:/new/prefix1/codeblack/02x (36).png);");
        break;
    case 36:ui->codelabel->setStyleSheet("border-image: url(:/new/prefix1/codeblack/02x (37).png);");
        break;
    case 37:ui->codelabel->setStyleSheet("border-image: url(:/new/prefix1/codeblack/02x (38).png);");
        break;
    case 38:ui->codelabel->setStyleSheet("border-image: url(:/new/prefix1/codeblack/02x (39).png);");
        break;
    case 99:ui->codelabel->setStyleSheet("border-image: url(:/new/prefix1/codeblack/02x (40).png);");
        break;
    }
}

//点击保存城市天气信息
void WeatherWindow::on_save_bnt_clicked()
{
    //发送当前天气界面保存的城市天气
    funSignal(ui->citylabel->text());
    //因为在跳转时，记录了上一个界面
    //所以可以利用此函数获取当前窗口的父窗口对象指针
    QWidget *parent = this->parentWidget();

    parent->show();//把之前父界面的隐藏状态改为显示状态，显示父界面

    this->close();//隐藏当前界--因为语音界面之创建了一次
}

//点击退出该界面，不保存城市天气信息
void WeatherWindow::on_cancel_btn_clicked()
{
    //因为在跳转时，记录了上一个界面
    //所以可以利用此函数获取当前窗口的父窗口对象指针
    QWidget *parent = this->parentWidget();

    parent->show();//把之前父界面的隐藏状态改为显示状态，显示父界面

    this->close();//隐藏当前界--因为语音界面之创建了一次
}

//点击城市条列，显示该城市的天气
void WeatherWindow::on_listWidget_itemClicked(QListWidgetItem *item)
{
    qDebug()<<"item"<<item->text();
    set_city(item->text());
}
